package netzwerk;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CopyOnWriteArrayList;

import model.Order;

public class OutgoingCustomer extends Thread {

	ObjectOutputStream out;
	String userName;
	String password;
	CopyOnWriteArrayList<Order> orders;
	Order order;


	public OutgoingCustomer() {

	}

	public OutgoingCustomer(ObjectOutputStream out, String name, String password,
			CopyOnWriteArrayList<model.Order> orders) {
		this.out = out;
		this.userName = name;
		this.password = password;
		this.orders = orders;
	}

	@Override
	public void run() {

		//Schreiben der Nutzereingabe
		while (true){
			
			while (!orders.isEmpty()) {
				System.out.println("Versuche in OutputStream zu schreiben");
				try {
					synchronized (this) {						
						out.writeObject(userName);
						out.writeObject(password);
					}
					out.writeObject(orders.remove(0));
					out.flush();
					this.userName = null;
					this.password = null;
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}

			synchronized (orders) {
				while (orders.isEmpty()) {
					try {
						orders.wait();
					}
					catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

	}
	
	public synchronized void setLoginData(String name, String password) {
		this.userName = name;
		this.password = password;
	}


}
